<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 贝贝 <hebiduhebi@163.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------
namespace Shop\Api;
use Common\Api\Api;
use Shop\Model\GroupattendModel;

class GroupattendApi extends Api{


    /**
     * 查询,不分页
     */
    const QUERY_NO_PAGING = "Shop/Groupattend/queryNoPaging";
    /**
     * 添加
     */
    const ADD = "Shop/Groupattend/add";
    /**
     * 保存
     */
    const SAVE = "Shop/Groupattend/save";
    /**
     * 保存根据ID主键
     */
    const SAVE_BY_ID = "Shop/Groupattend/saveByID";

    /**
     * 删除
     */
    const DELETE = "Shop/Groupattend/delete";

    /**
     * 查询
     */
    const QUERY = "Shop/Groupattend/query";
    /**
     * 查询一条数据
     */
    const GET_INFO = "Shop/Groupattend/getInfo";
    /**
     * 查询条数
     */
    const GET_COUNT = "Shop/Groupattend/getcount";
    /**
     * 查询类目属性的值表
     */
    const QUERY_PROP_TABLE = "Shop/Groupattend/queryPropTable";
    /**
     * 查询类目属性的值表
     */
    const QUERY_GROUP = "Shop/Groupattend/querygroup";
    /**
     * 查询类目属性的值表
     */
    const QUERY_GROUPDETAIL = "Shop/Groupattend/querygroupdetail";
    const GROUPDETAIL = "Shop/Groupattend/groupdetail";
    const PEOPLE_ATTEND = "Shop/Groupattend/people_attend";



    protected function _init(){
        $this->model = new GroupattendModel();
    }

    //获取参团人数
    public function getcount($map){
        $result = $this -> model -> where($map) -> count('id');
        if ($result === false) {
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        } else {
            return $this -> apiReturnSuc($result);
        }

    }

    //参团人员
    public function people_attend($map){
        $query= $this -> model -> where($map);

        $query=$query->alias('ga')
            ->join('left join __WXUSER__ as wu on ga.uid=wu.id')
            ->field('ga.id as attendid,ga.p_id,wu.id,wu.nickname,wu.avatar')
            ->select();
        return $this -> apiReturnSuc($query);


    }



    //拼团匹配
    public function querygroup($map=null,$ma=null){


            $query= $this -> model -> where($map);

            $query=$query->alias('ga')
                ->join('join __GROUP__ as g on ga.groupid=g.id')
                ->join('left join __ORDERS_ITEM__ as oi on ga.ordercode=oi.order_code')

                ->join('left join __PRODUCT_GROUP__ as pg on ga.p_id=pg.p_id')
                ->field('ga.id as attendid,g.id,g.group_sum,g.status,g.uid,oi.name,oi.img,oi.price,pg.end_time,ga.ordercode')
                ->select();
            return($query);



    }



    //查询拼团详情
    public function querygroupdetail($id){
        $map['ga.id']=$id;
        $query= $this -> model -> where($map);
        $query=$query->alias('ga')
            ->join('left join __GROUP__ as g on ga.groupid=g.id')
            ->join('left join __ORDERS_ITEM__ as oi on ga.ordercode=oi.order_code')
            ->join('left join __PRODUCT_GROUP__ as pg on ga.p_id=pg.p_id')
            ->join('left join __PRODUCT__ as p on ga.p_id=p.id')

            ->field('g.id,g.group_sum,g.group_count,g.status,g.uid,oi.name,oi.img,oi.price,pg.end_time,p.synopsis,ga.p_id')
            ->find();


        $groupid=$query['id'];
        $ma['ga.groupid']=$groupid;
        $people=$this -> model -> where($ma)->alias('ga')
                ->join('left join __WXUSER__ as wu on ga.uid=wu.id')
                ->field('ga.createtime,ga.id,ga.uid,ga.status,wu.nickname,wu.avatar')
                ->select();
        foreach($people as $k =>$v){
            $people[$k]['createtime']=date('Y-m-d H:i:s',$v['createtime']);
        }

        return(array('query'=>$query,'people'=>$people));
    }


    public function groupdetail($id){
//        $map['ga.groupid']=$id;
//        $query= $this -> model -> where($map);
//        $query=$query->alias('ga')
//            ->join('left join __GROUP__ as g on ga.groupid=g.id')
//            ->join('left join __ORDERS_ITEM__ as oi on ga.ordercode=oi.order_code')
//            ->join('left join __PRODUCT_GROUP__ as pg on ga.p_id=pg.p_id')
//            ->join('left join __PRODUCT__ as p on ga.p_id=p.id')
//            ->field('g.id,g.group_sum,g.group_count,g.status,g.uid,oi.name,oi.img,oi.price,pg.end_time,p.synopsis,ga.p_id,ga.ordercode')
//            ->find();
//
//
//        $groupid=$query['id'];
        $ma['ga.groupid']=$id;
        $people=$this -> model -> where($ma)->alias('ga')
            ->join('left join __WXUSER__ as wu on ga.uid=wu.id')
            ->join('left join __ORDERS__ as o on ga.ordercode=o.order_code')
            ->field('ga.ordercode,ga.createtime,ga.id,ga.uid,ga.groupid,wu.nickname,wu.avatar,o.id as orderid,o.pay_type,o.pay_status')
            ->select();
//        foreach($people as $k =>$v){
//            $people[$k]['createtime']=date('Y-m-d H:i:s',$v['createtime']);
//        }
        return(array('status'=>true,'people'=>$people));
        //return(array('query'=>$query,'people'=>$people));
    }










}


